package com.mall.content.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.mall.content.service.ContentCategoryService;
import com.mall.manager.dao.TbContentCategoryMapper;
import com.mall.manager.pojo.TbContentCategory;
import com.mall.manager.pojo.TbContentCategoryExample;
import com.mall.pojo.EasyUiTreeNode;
import com.mall.pojo.TaotaoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Transactional
@Component
public class ContentCategoryServiceImp implements ContentCategoryService {
    
    @Autowired
    private TbContentCategoryMapper contentCategoryMapper;
    
    @Override
    public List<EasyUiTreeNode> showContentCategory(long parentId) {

        TbContentCategoryExample contentCategoryExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = contentCategoryExample.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> contentCategories = contentCategoryMapper.selectByExample(contentCategoryExample);

        List<EasyUiTreeNode> easyUiTreeNodes = new ArrayList<>();
        //转化成easyUI菜单树pojo
        for (TbContentCategory contentCategory : contentCategories) {
            EasyUiTreeNode easyUiTreeNode = new EasyUiTreeNode();
            easyUiTreeNode.setId(contentCategory.getId());
            easyUiTreeNode.setText(contentCategory.getName());
            easyUiTreeNode.setState(contentCategory.getIsParent()?"closed":"open");
            easyUiTreeNodes.add(easyUiTreeNode);
        }
        return easyUiTreeNodes;
    }

    @Override
    public TaotaoResult createContentCategory(long parentId, String name) {
        TbContentCategoryExample contentCategoryExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = contentCategoryExample.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        //获得父节点
        List<TbContentCategory> parentCategory = contentCategoryMapper.selectByExample(contentCategoryExample);
        for (TbContentCategory pcg : parentCategory) {
            //更新父节点的信息
            if (!pcg.getIsParent()){
                pcg.setIsParent(true);
            }
        }
        TbContentCategory contentCategory = new TbContentCategory();
        Date date = new Date();
        contentCategory.setIsParent(false);
        contentCategory.setCreated(date);
        contentCategory.setUpdated(date);
        contentCategory.setStatus(1);
        contentCategory.setName(name);
        contentCategory.setParentId(parentId);
        contentCategory.setSortOrder(1);
        //由于设置了获取上次的最后一次插入id值，因此插入完成之后contentCategory的id值会更新
        contentCategoryMapper.insert(contentCategory);

        //返回一个真正的id，更新内容分类树
        TaotaoResult taotaoResult = new TaotaoResult();
        taotaoResult.setStatus(200);
        taotaoResult.setData(contentCategory);
        return taotaoResult;
    }

    @Override
    public void UpdateContentCategory(long id, String name) {
        TbContentCategoryExample contentCategoryExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = contentCategoryExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<TbContentCategory> contentCategories = contentCategoryMapper.selectByExample(contentCategoryExample);
        TbContentCategory contentCategory = contentCategories.get(0);
        contentCategory.setName(name);
        contentCategoryMapper.updateByExample(contentCategory,contentCategoryExample);
    }

    @Override
    public void deleteContentCategory(long id) {
        contentCategoryMapper.deleteByPrimaryKey(id);
    }

}
